﻿using System;
using System.Collections.Generic;
using System.IO;

namespace DataGeneration.Domain.Helpers
{
    /// <summary>
    /// Defines the type FileHelper
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public static void DeleteFile(string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
        }

        /// <summary>
        /// Creates the file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public static void CreateFile(string filename)
        {
            using (StreamWriter writer = new StreamWriter(filename, true))
            { }
        }

        /// <summary>
        /// Creates the folder.
        /// </summary>
        /// <param name="folderName">Name of the folder.</param>
        public static void CreateFolder(string folderName)
        {
            if (Directory.Exists(folderName))
            {
                Directory.Delete(folderName, true);
            }
            Directory.CreateDirectory(folderName);
        }

        /// <summary>
        /// Saves to file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="content">The content.</param>
        public static void SaveToFile(string filename, string content)
        {
            using (StreamWriter writer = new StreamWriter(filename, true))
            {
                writer.WriteLine(content);
            }
        }

        /// <summary>
        /// Saves the entity key.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="content">The content.</param>
        public static void SaveEntityKey(string filename, string content)
        {
            if (EntityKeyExist(filename,content))
            {
                throw new Exception("Entity Key must be unique!");
            }

            using (StreamWriter writer = new StreamWriter(filename, true))
            {
                writer.WriteLine(content);
            }
        }

        /// <summary>
        /// Entities the key exist.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="content">The content.</param>
        /// <returns></returns>
        public static bool EntityKeyExist(string filename, string content)
        {
            return ReadFileContent(filename).Contains(content);
        }

        /// <summary>
        /// Reads the content of the file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public static IList<string> ReadFileContent(string filename)
        {
            IList<string> content = new List<string>();
            using (var reader = new StreamReader(filename))
            {
                while (reader.Peek() > -1)
                {
                    content.Add(reader.ReadLine());
                }
            }
            return content;
        }

        /// <summary>
        /// Reads the content of the file.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public static IList<string> ReadFileContent(Stream stream)
        {
            IList<string> content = new List<string>();
            using (var reader = new StreamReader(stream))
            {
                while (reader.Peek() > -1)
                {
                    content.Add(reader.ReadLine());
                }
            }
            return content;
        }

        /// <summary>
        /// Reads the content of the unique file.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public static IList<string> ReadUniqueFileContent(Stream stream)
        {
            IList<string> content = new List<string>();
            using (var reader = new StreamReader(stream))
            {
                while (reader.Peek() > -1)
                {
                    var value = reader.ReadLine();
                    if (!string.IsNullOrWhiteSpace(value) && !content.Contains(value))
                    {
                        content.Add(value);
                    }
                }
            }
            return content;
        }


        /// <summary>
        /// Gets the next key.
        /// </summary>
        /// <param name="entityKeyFilename">The entity key filename.</param>
        /// <returns></returns>
        public static int GetNextKey(string entityKeyFilename)
        {
            int nextKey = 1;
            string lastEntry = ReadLastEntry(entityKeyFilename);
            if (!lastEntry.Trim().Equals(""))
            {
                nextKey = Convert.ToInt32(lastEntry) + 1;
            }

            return nextKey;
        }

        /// <summary>
        /// Reads the last key.
        /// </summary>
        /// <param name="entityKeyFilename">The entity key filename.</param>
        /// <returns></returns>
        public static string ReadLastKey(string entityKeyFilename)
        {
            return ReadLastEntry(entityKeyFilename);
        }

        /// <summary>
        /// Reads the last entry.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public static string ReadLastEntry(string filename)
        {
            string lastEntry = "";
            try
            {
                using (StreamReader reader = new StreamReader(filename))
                {
                    while (reader.Peek() > -1)
                    {
                        lastEntry = reader.ReadLine();
                    }
                }
            }
            catch (FileNotFoundException)
            {
                CreateFile(filename);
            }
            return lastEntry;
        }

        /// <summary>
        /// Reads the last integer key.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public static int ReadLastIntegerKey(string filename)
        {
            int lastEntry = 0;
            try
            {
                using (StreamReader reader = new StreamReader(filename))
                {
                    while (reader.Peek() > -1)
                    {
                        lastEntry = Convert.ToInt32(reader.ReadLine());
                    }
                }
            }
            catch (FileNotFoundException)
            {
                CreateFile(filename);
            }
            return lastEntry;
        }
    }
}


